package br.com.jmmarca.dao.generic;

import br.com.jmmarca.controller.R;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.SystemClock;

public class DataManager {

	private Context context;
	private SQLiteDatabase database;
	private String database_name;
	private Integer database_version;	

	
	public DataManager(Context context){
		setContext(context);
		database_name = context.getString(R.string.app_name);
		database_version = Integer.parseInt(context.getString(R.string.database_versao));
		
		SQLiteOpenHelper openHelper = new OpenHelper(context, database_name, null, database_version);
		setDatabase(openHelper.getWritableDatabase());		
		

	}
	
	private void openDb(){
		if(! getDatabase().isOpen()){
			setDatabase(SQLiteDatabase.openDatabase(Environment.getDataDirectory() + "/data/"+database+".db", null, SQLiteDatabase.OPEN_READWRITE));
		}
	}
	
	private void closeDb() {
		if (getDatabase().isOpen()) {
			getDatabase().close();
	    }
	}

	private void resetDb() {      
		closeDb();
	    SystemClock.sleep(500);
	    openDb();
	}
		
	public Context getContext() {
		return context;
	}
	public void setContext(Context context) {
		this.context = context;
	}
	public SQLiteDatabase getDatabase() {
		return database;
	}
	public void setDatabase(SQLiteDatabase database) {
		this.database = database;
	}

	
}
